<template>
    <view class="page-wrapper">
        <image :src="deviceImg" mode="aspectFit" class="device-image" />

        <view class="shorcut-list">
            <view class="shorcut-item" v-for="item in dataList" :key="item.id">
                <view class="info">
                    <view class="time">{{ item.time }}</view>
                    <view class="desc-text">&nbsp;{{ item.fenshu }}&nbsp;{{ $t('common.feedUnit') }}</view>
                </view>
                <view class="switch-wrapper">
                    <up-switch v-model="item.active" activeColor="#15B8A6" @change="changeSwitch(item)"></up-switch>
                </view>
            </view>
        </view>

        <view class="button-wrapper shadow-top">
            <view class="button-box" @click="navigateToUniPage(`/pages/machine/plan?imei=${ deivceId }`)">
                <view class="icon">
                    <uni-icons type="calendar-filled" size="26"></uni-icons>                    
                </view>
                <view>{{ $t('pages.machine_index.FeedingPlan') }}</view>
            </view>
            <view class="feed-button" @click="showPupup">
                <uni-icons type="paperplane-filled" color="#FFFFFF" size="32"></uni-icons> 
                <text>{{ $t('pages.machine_index.ManualFeed') }}</text>
            </view>
            <view class="button-box" @click="navigateToUniPage(`/pages/machine/log?imei=${ deivceId }&img=${ encodeURIComponent(deviceImg) }`)">
                <view class="icon">
                    <uni-icons type="list" size="26"></uni-icons>
                </view>
                <view>{{ $t('pages.machine_index.FeedingLog') }}</view>
            </view>
        </view>

        <uni-popup ref="showBottom" type='bottom'>
            <view class="bottom-popup">
                <view class="top-row">
                    <text class="title">{{ $t('pages.machine_index.ManualFeed') }}</text>
                </view>

                <picker-view class="picker-wrapper" indicator-style="height: 80rpx;" @change="onFeedNumChange">
                    <picker-view-column>
                        <view class="picker-item" v-for="item in 9" :key="item">{{ item }} {{ $t('common.feedUnit') }}</view>
                    </picker-view-column>
                </picker-view>

                <view class="bottom-box">
                    <up-button :text="$t('common.Feeding')" size="normal" type="primary" 
                        @click="manualFeed" class="bottom-button middle-round" ></up-button>
                </view>
            </view>
        </uni-popup>
    </view>
</template>

<script setup>
import { ref, } from 'vue';
import { onLoad, onShow, } from '@dcloudio/uni-app'
import { getClockListApi, changeWeiShiApi, } from "@/api/request";
import { CacheUtils, debounceFunction, navigateToUniPage, } from '@/utils/index';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
const deivceId = ref('');
const deviceImg = ref('');
const dataList = ref([]);
const manualFeedNum = ref(1);
const showBottom = ref(null);
const hasShowErrorToast = ref(false); // 是否已经展示异常提示框

const showPupup = () => {
    showBottom.value?.open();
}
const hidePoup = () => {
    showBottom.value?.close();
}
const onFeedNumChange =({ detail })=>{
	manualFeedNum.value = detail.value[0]+1;
}

const getFeedList = async () => {
    let query = {
        userid: CacheUtils.get('userid'),
        imei: deivceId.value
    }
    let res = await getClockListApi(query)
    if (res.response.result == true) {
        dataList.value = (res.list || []).map(el => ({
            ...el,
            active: el.on == 1 ? true : false
        }));
    } else {
        dataList.value = []
    }
}



const changeWeiShi = async (params) => {
    let res = await changeWeiShiApi(params)
    if (res.response.result == true) {
        let paramss = {
            on: params.on,
            fenshu: params.fenshu,
            xiaoshi: (params.time.split(':')[0]),
            fenzhong: params.time.split(':')[1],
            fenzuid: params.fenzuid,
        }

        uni.showLoading({
            title: t('common.instructSendingTitle'),
            mask: true,
        });
        uni.$emit('weishishijian', paramss)
        uni.$on("weishiPlanSuccess", () => {
            uni.hideLoading()
        });
        uni.$once('weishiPlanError', () => {
            uni.hideLoading()            
            !hasShowErrorToast.value && uni.showModal({
                title: t('common.instructFailedTitle'),
                showCancel: false,
                content: t('common.instructFailedDesc'),
				success(){
					hasShowErrorToast.value = false;					
				},
            });
			setTimeout(()=>{
				hasShowErrorToast.value = true;				
			},200);
        });
    } else {
        uni.showToast({
            title: res.response.msg,
            icon: 'none'
        })
    }
}

const debounceEdit = debounceFunction(changeWeiShi);
// 开关状态切换
const changeSwitch = (item) => {
    let query = {
        id: item.id,
        time: item.time,
        week: item.week,
        fenshu: item.fenshu,
        remark: item.remark,
        on: item.active ? 1 : 0,
        fenzuid: item.fenzuid,
    }

    debounceEdit(query);
}

// 手动喂食
const manualFeed = () => {
    uni.showLoading({
        mask: true,
        title: t('common.instructSendingTitle')
    });
    setTimeout(() => {
        uni.hideLoading();
		manualFeedNum.value = 1;
        hidePoup();
    }, 2000);
    uni.$emit('weishi', manualFeedNum.value);
}

onLoad((query) => {
    uni.setNavigationBarTitle({
        title: query.title
    });
    deivceId.value = query.imei;
	deviceImg.value = decodeURIComponent(query.img); 
})

onShow(()=>{
    getFeedList();
})

</script>

<style lang="scss" scoped>
$bottomHeight: 200rpx;

.page-wrapper {
    padding-bottom: $bottomHeight;
    overflow-y: auto;
    position: relative;
    background-color: #FFFFFF;

    .button-wrapper {
        background-color: #FFFFFF;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: $bottomHeight;
        box-sizing: border-box;
        display: flex;
        padding: 20rpx 30rpx;
        justify-content: space-between;
        align-items: center;

        .button-box {
            width: 220rpx;
            display: flex;
            flex-direction: column;
            align-items: center;
            row-gap: 4rpx;
            font-size: 24rpx;

            .icon {
                
            }
        }

        .feed-button {
            width: 230rpx;
            height: 160rpx;
            background-color: $uni-primary;
            border-radius: 40rpx;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            color: #FFFFFF;
            font-size: 26rpx;
        }
    }
}

.device-image {
    display: block;
    width: 380rpx;
    height: 380rpx;
    margin: 60rpx auto;
    margin-bottom: 40rpx;
}

.shorcut-list {
    padding: 30rpx;
    display: flex;
    flex-direction: column;
    row-gap: 30rpx;

    .shorcut-item {
        padding: 30rpx 40rpx;
        background-color: #FFFFFF;
        display: flex;
        align-items: center;
        column-gap: 10rpx;
        border-radius: 20rpx;
        box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.1);

        .info {
            flex: 1;

            .time {
                color: #000000;
                font-size: 60rpx;
            }
        }

        .switch-wrapper {
            width: 120rpx;
        }
    }
}

.bottom-popup{
    width: 750rpx;
    height: 600rpx;
    padding: 0 20rpx;
    border-radius: 40rpx 40rpx 0 0;
    background-color: #fff;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: center;
    .top-row{
        margin-top: 20rpx;
        display: flex;
        justify-content: space-between;
        .title{
            flex: 1;
            text-align: center;
            font-size: 40rpx;
        }
        
    }
    .picker-wrapper{
        position: relative;
        margin-top: 30rpx auto;
        height: 600rpx;
        width: 700rpx;
        text-align: center;
        overflow: hidden;
        .picker-item{
            display: flex;
            align-items: center;
            justify-content: center;
        }

    }
    .bottom-box{
        padding: 10rpx 20rpx;
        .bottom-button{
            margin: 0 auto;
            width: 560rpx;
        }
    }
}
</style>